package com.evl.task;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.evl.common.persistence.dao.StationMapper;
import com.evl.common.persistence.dao.VehicleMapper;
import com.evl.common.persistence.model.Vehicle;
import com.evl.common.persistence.model.VehicleSchedule;
import com.evl.dictionaryConfig.DictConfig;
import com.evl.dictionaryConfig.DictConst;
import com.evl.utils.DateUtils;
import com.evl.vehicleSchedule.dao.VehicleScheduleDao;
import com.evl.vehicleSchedule.service.IVehicleScheduleService;

@Component
public class TrainScheduleTask {
	
	@Resource
	VehicleScheduleDao vehicleScheduleDao;
	
	@Resource
	VehicleMapper vehicleMapper;
	
	@Resource
	StationMapper stationMapper;
	
	@Resource
	IVehicleScheduleService vehicleScheduleService;

	private Logger log = Logger.getLogger(this.getClass());
	
	/**
	 * 后台定时生成调度车次 （30天后）
	 * 单线程 --(多线程需要要自行编写实现)
	 */
	@Scheduled(cron="0 30 0 1/1 * ?") //间隔1天，每天0点30分执行
	//@Scheduled(cron="0 0/1 * * * ?") //测试
	public void createTrainSchedule() {

		List<String> goList = new ArrayList<String>(); //去程日期list
    	Date standardDate = new Date();
		try {
			standardDate = DateUtils.getDateByTimeDD(DictConfig.getKey(DictConst.schedule_standard_date)); //以此日期作为去程(西宁至德令哈 )日期的基准，返程+1
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Date curDate = DateUtils.getPreviousOrNextDaysOfDate(new Date(),30);//30可做成配置项
		
		//获取模板数据
    	List<Vehicle> vehicleList = vehicleMapper.selectVehicles(null);
		
		try {
			for (Vehicle vehicle : vehicleList){
				
				VehicleSchedule vSchedule = vehicleScheduleDao.getVehicleScheduleByTime(vehicle.getVehicleNo(), DateUtils.getSpecifyDate(curDate, DateUtils.FORMAT_YYYY_MM_DD));
				if(vSchedule != null){//如果已经存在不生成
					continue;
				}
				//定义调度车次list
		    	List<VehicleSchedule> schedulelist = new ArrayList<VehicleSchedule>();
				
				//计算调度日期与基准日期的天数差
				int days =DateUtils.daysBetween(standardDate,curDate);
				if(days%2 == 0){ //去程（隔日发车）
					
					goList.add(DateUtils.getSpecifyDate(curDate, DateUtils.FORMAT_YYYY_MM_DD));
					
			    	//批量生成去程的车次信息
			    	for ( String goDate : goList){
			    		VehicleSchedule vehicleSchedule =new VehicleSchedule();
			    		
			    		vehicleSchedule.setVehicleType(vehicleList.get(0).getVehicleType());//车辆类型
			    		vehicleSchedule.setVehicleNo(vehicleList.get(0).getVehicleNo());//车次
			    		try {
							vehicleSchedule.setDepartTime(DateUtils.getDateByTimeMM(goDate+" "+vehicleList.get(0).getDepartTime()));//去程发车时间
						} catch (ParseException e) {
							e.printStackTrace();
						}
			    		vehicleSchedule.setCostTime(vehicleList.get(0).getCostTime());//耗时
			    		vehicleSchedule.setArrTime(DateUtils.getPreviousOrNextMinutesOfDate(vehicleSchedule.getDepartTime(), Integer.parseInt(vehicleSchedule.getCostTime())));//去程到达时间
			    		vehicleSchedule.setDeparture(vehicleList.get(0).getDeparture());//始发站
			    		vehicleSchedule.setTerminus(vehicleList.get(0).getTerminus());//终点站
			    	
			    		schedulelist.add(vehicleSchedule);
			    	}
			    	
				}
				
				//批量生成调度车次（车次、车站、座位、车站座位关系、余票）
				vehicleScheduleService.createSchedules(vehicle, schedulelist);
			}
			log.info("调度生成车次信息成功");
		} catch (Exception e) {
			log.error("调度生成车次信息失败", e);
		}
	}
}
